package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.dto.TbClueTrackRecordDto;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 线索跟进记录Service业务层处理
 *
 * @author LiuYongDong
 * @date 2022-11-03
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {


    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;

    @Autowired
    private TbAssignRecordMapper tbAssignRecordMapper;

    @Autowired
    private ITbClueService iTbClueService;

    /**
     * 线索跟进记录列表
     *
     * @param clueId 线索id
     * @return 线索跟进记录集合
     */
    @Override
    public List<TbClueTrackRecordDto> getClueTrackRecord(Long clueId) {
        return tbClueTrackRecordMapper.getClueTrackRecord(clueId);
    }

    /**
     * 添加线索跟进记录
     *
     * @param tbClueTrackRecordDto 线索跟进记录对象
     * @return 添加是否成功
     */
    @Override
    public Boolean addClueTrackRecord(TbClueTrackRecordDto tbClueTrackRecordDto) {
        //创建一个新的线索对象(TbClue)
        TbClue tbClue = new TbClue();
        //获取创建人
        String username = SecurityUtils.getUsername();

        //获取传入参数中的信息
        Long id = tbClueTrackRecordDto.getClueId();
        String name = tbClueTrackRecordDto.getName();
        String sex = tbClueTrackRecordDto.getSex();
        Integer age = tbClueTrackRecordDto.getAge();
        String weixin = tbClueTrackRecordDto.getWeixin();
        String qq = tbClueTrackRecordDto.getQq();
        String level = tbClueTrackRecordDto.getLevel();
        String subject = tbClueTrackRecordDto.getSubject();
        Date nextTime = tbClueTrackRecordDto.getNextTime();
        String createBy = tbClueTrackRecordDto.getCreateBy();
        Date createTime = tbClueTrackRecordDto.getCreateTime();
        //将参数中的信息注入tbClue中
        tbClue.setCreateTime(createTime);
        tbClue.setCreateBy(createBy);
        tbClue.setId(id);
        tbClue.setName(name);
        tbClue.setSex(sex);
        tbClue.setAge(age);
        tbClue.setWeixin(weixin);
        tbClue.setQq(qq);
        tbClue.setLevel(level);
        tbClue.setSubject(subject);
        tbClue.setNextTime(nextTime);
        tbClue.setUpdateBy(username);
        tbClue.setUpdateTime(new Date());
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        //调用Mapper更新tb_clue表的数据
        try {
            tbClueMapper.updateTbClue(tbClue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将参用户信息和当前日期注入跟进对象tbClueTrackRecordDto中
        tbClueTrackRecordDto.setCreateBy(username);
        tbClueTrackRecordDto.setCreateTime(new Date());

        return tbClueTrackRecordMapper.addClueTrackRecord(tbClueTrackRecordDto);
    }



    @Override
    public Integer setFalseClue(Long id, Map map) {
        TbClue tbClue = iTbClueService.selectTbClueById(id);
        int falseCount = tbClue.getFalseCount();
        if (falseCount >=3) {
            //比较如果说被标记了3次，删除条线索 delete删除
            tbClueTrackRecordMapper.deleteCuleTrackByid(id);
            tbAssignRecordMapper.deleteAssignRecordByAssignId(id,"0");
            return iTbClueService.deleteTbClueById(id);
        }
        //修改线索的状态，线索的失败次数+1
        tbClue.setFalseCount(falseCount+1);
        tbClue.setStatus(TbClue.StatusType.FALSE.getValue());//状态伪线索
        tbClue.setNextTime(null);
        tbClue.setUpdateTime(DateUtils.getNowDate());
        iTbClueService.updateTbClue(tbClue);
        //删除线索分配记录
        tbAssignRecordMapper.deleteAssignRecordByAssignId(id,"0");

        //添加线索跟进记录
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        tbClueTrackRecord.setCreateBy(SecurityUtils.getUsername());//操作人
        tbClueTrackRecord.setClueId(id);//线索id
        tbClueTrackRecord.setSubject(tbClue.getSubject());//获取课程类型
        tbClueTrackRecord.setLevel(tbClue.getLevel());//设置意向等级
        tbClueTrackRecord.setType("1");//0 正常跟进记录 1 伪线索
        tbClueTrackRecord.setNextTime(null);//下次跟进的时间
        tbClueTrackRecord.setCreateTime(DateUtils.getNowDate());//创建时间
        tbClueTrackRecord.setFalseReason((String) map.get("reason"));//失败的原因
        tbClueTrackRecord.setRecord((String) map.get("remark"));//失败的备注
        Integer result = tbClueTrackRecordMapper.insert(tbClueTrackRecord);
        return result;
    }
}
